'use strict';

const Controller = require('egg').Controller;

/**
 * 投稿管理控制器
 * 处理投稿的增删改查、审核、打分等功能
 */
class ContributionController extends Controller {
  /**
   * 获取投稿列表
   * 支持多条件筛选和分页查询
   * @param {Object} ctx.request.body
   * @param {number} [ctx.request.body.page=1] - 页码
   * @param {number} [ctx.request.body.page_size=10] - 每页条数
   * @param {string} [ctx.request.body.title] - 标题，模糊匹配
   * @param {string} [ctx.request.body.author] - 作者姓名，模糊匹配任意作者
   * @param {number} [ctx.request.body.project_group_id] - 项目组ID
   * @param {number} [ctx.request.body.approved_status] - 审核状态：0-未审核，1-已审核，2-未采用
   * @param {string} [ctx.request.body.start_date] - 开始日期，格式：YYYY-MM-DD
   * @param {string} [ctx.request.body.end_date] - 结束日期，格式：YYYY-MM-DD
   */
  async list() {
    const { ctx } = this;
    
    // 验证查询参数
    ctx.validate({
      page: { type: 'number', required: false, min: 1 },
      page_size: { type: 'number', required: false, min: 1, max: 100 },
      title: { type: 'string', required: false },
      author: { type: 'string', required: false },
      project_group_id: { type: 'number', required: false },
      approved_status: { type: 'number', required: false, min: 0, max: 2 },
      start_date: { type: 'string', required: false, format: /^\d{4}-\d{2}-\d{2}$/ },  // YYYY-MM-DD格式
      end_date: { type: 'string', required: false, format: /^\d{4}-\d{2}-\d{2}$/ },    // YYYY-MM-DD格式
    }, ctx.request.body);

    const result = await ctx.service.contribution.findAll(ctx.request.body);
    ctx.body = result;
  }

  // 获取单个投稿
  async getById() {
    const { ctx } = this;
    ctx.validate({
      contribution_id: { type: 'number', required: true },
    });

    const { contribution_id } = ctx.request.body;
    const contribution = await ctx.service.contribution.findById(contribution_id);
    if (!contribution) {
      ctx.throw(404, '未找到该投稿');
    }
    ctx.body = contribution;
  }

  // 创建投稿
  async create() {
    const { ctx } = this;
    ctx.validate({
      title: { type: 'string', required: true },
      author1: { type: 'number', required: true },
      author2: { type: 'number', required: false },
      author3: { type: 'number', required: false },
      remarks: { type: 'string', required: false },
      video_file_path: { type: 'string', required: false },
      project_group_id: { type: 'number', required: false },
    });

    const result = await ctx.service.contribution.create(ctx.request.body);
    if (result.error === 'author1_not_found') {
      ctx.throw(422, '未找到作者1');
    }
    if (result.error === 'author2_not_found') {
      ctx.throw(422, '未找到作者2');
    }
    if (result.error === 'author3_not_found') {
      ctx.throw(422, '未找到作者3');
    }
    ctx.body = result;
  }

  // 更新投稿
  async update() {
    const { ctx } = this;
    ctx.validate({
      contribution_id: { type: 'number', required: true },
      title: { type: 'string', required: false },
      author1: { type: 'number', required: false },
      author2: { type: 'number', required: false },
      author3: { type: 'number', required: false },
      remarks: { type: 'string', required: false },
      video_file_path: { type: 'string', required: false },
      project_group_id: { type: 'number', required: false },
      approved_status: { type: 'number', required: false, min: 0, max: 2 },
      score: { type: 'number', required: false, min: 0 },
      extra_score: { type: 'number', required: false, min: 0 },
      tiktok_likes: { type: 'number', required: false, min: 0 },
      channels_views: { type: 'number', required: false, min: 0 },
      channels_likes: { type: 'number', required: false, min: 0 },
    });

    const result = await ctx.service.contribution.update(
      ctx.request.body.contribution_id,
      ctx.request.body
    );

    if (!result) {
      ctx.throw(404, '未找到该投稿');
    }
    if (result.error === 'author1_not_found') {
      ctx.throw(422, '未找到作者1');
    }
    if (result.error === 'author2_not_found') {
      ctx.throw(422, '未找到作者2');
    }
    if (result.error === 'author3_not_found') {
      ctx.throw(422, '未找到作者3');
    }

    ctx.body = result;
  }

  // 删除投稿
  async delete() {
    const { ctx } = this;
    ctx.validate({
      contribution_id: { type: 'number', required: true },
    });

    const { contribution_id } = ctx.request.body;
    const result = await ctx.service.contribution.delete(contribution_id);
    if (!result) {
      ctx.throw(404, '未找到该投稿');
    }
    ctx.body = result;
  }

  /**
   * 审核投稿
   * 只能审核一次，审核后不能修改状态
   * @param {Object} ctx.request.body
   * @param {number} ctx.request.body.contribution_id - 投稿ID
   * @param {number} ctx.request.body.approved_status - 审核状态：1-已审核，2-未采用
   * @param {number} ctx.request.body.approved_remark - 审核意见
   * @param {string} ctx.request.body.title - 标题
   * @throws {404} 未找到投稿时抛出异常
   * @throws {422} 重复审核时抛出异常
   */
  async approve() {
    const { ctx } = this;
    ctx.validate({
      contribution_id: { type: 'number', required: true },
      approved_status: { type: 'number', required: true, min: 1, max: 2 }, // 1-已审核、2-未采用
      title: { type: 'string', required: true }, 
    });

    // 从登录用户信息中获取审核人信息
    const { person_id, person_name } = ctx.state.user;

    const result = await ctx.service.contribution.approve(
      ctx.request.body.contribution_id,
      ctx.request.body.approved_status,
      ctx.request.body.approved_remark,
      person_id,
      person_name,
      ctx.request.body.title
    );

    if (result.error === 'not_found') {
      ctx.throw(404, '未找到该投稿');
    }

    if (result.error === 'already_approved') {
      ctx.throw(422, '该投稿已审核，不能重复审核');
    }

    ctx.body = result;
  }

  /**
   * 投稿打分
   * 只能对审核通过的投稿打分，根据启用的规则组计算各作者得分
   * @param {Object} ctx.request.body
   * @param {number} ctx.request.body.contribution_id - 投稿ID
   * @param {number} ctx.request.body.score - 分数
   * @throws {404} 未找到投稿时抛出异常
   * @throws {422} 投稿未审核通过时抛出异常
   * @throws {422} 未找到启用的规则组时抛出异常
   * @throws {422} 未找到匹配的作者人数规则时抛出异常
   * @throws {422} 规则权重之和不等于1时抛出异常
   */
  async score() {
    const { ctx } = this;
    ctx.validate({
      contribution_id: { type: 'number', required: true },
      score: { type: 'number', required: true, min: 0 },
    });

    // 从登录用户信息中获取打分人ID
    const { person_id } = ctx.state.user;

    const result = await ctx.service.contribution.score(
      ctx.request.body.contribution_id,
      ctx.request.body.score,
      person_id
    );

    if (result.error === 'not_found') {
      ctx.throw(404, '未找到该投稿');
    }
    if (result.error === 'not_approved') {
      ctx.throw(422, '该投稿未审核通过，不能打分');
    }
    if (result.error === 'no_enabled_rule') {
      ctx.throw(422, '未找到启用的规则组，请先启用规则组');
    }
    if (result.error === 'no_matching_rule') {
      ctx.throw(422, '未找到匹配的作者人数规则');
    }
    if (result.error === 'invalid_weights') {
      ctx.throw(422, '规则权重之和不等于1');
    }

    ctx.body = result;
  }
}

module.exports = ContributionController; 